package com.changgou.web.center.controller;


import com.changgou.entity.Result;
import com.changgou.order.feign.OrderFeign;
import com.changgou.order.pojo.Order;
import com.changgou.pay.feign.PayFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/wcenterorder")
public class WCenterOrderController {

    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private PayFeign payFeign;


    /**
     * 获取订单信息到页面
     * @param model
     * @return 返回到页面展示
     */
    @RequestMapping("/list")
    public String orderList(Model model){

        //通过feign远程调用订单微服务获取订单信息
        List<Order> orderList = orderFeign.list();

        model.addAttribute("orderList",orderList);

        return "center-index";
    }

    /**
     * 根据订单id付款下单
     * @param orderId 订单id
     * @param model
     * @return 返回微信二维码页面
     */
    @RequestMapping("/centerNativePay")
    public String centerNativePay(@RequestParam("orderId")String orderId, Model model) throws Exception {

        //通过feign远程调用技术, 调用支付业务服务的预支付接口
        Map resultMap = payFeign.centerNativePay(orderId);
        //预支付链接
        model.addAttribute("code_url", resultMap.get("code_url"));
        //支付订单的订单号
        model.addAttribute("orderId", resultMap.get("orderId"));
        //总支付金额
        model.addAttribute("payMoney", Integer.parseInt(String.valueOf(resultMap.get("totalMoney"))));

        //跳转到二维码支付页面

        return "wxpay";
    }

    /**
     * 取消订单
     * @return
     */
    @RequestMapping("/cancelOrder")
    public String cancelOrder(@RequestParam("orderId")String orderId){

        //根据订单id远程调用订单微服务取消订单
        orderFeign.delete(orderId);

        return "redirect:http://web.changgou.com:8001/api/wcenterorder/list";
    }

    /**
     * 待付款订单
     * @return
     */
    @RequestMapping("/waitPay")
    public String waitPayList(Model model){

        //远程调用订单微服务,获取所有未付款订单
        List<Order> orderList = orderFeign.waitPay();

        model.addAttribute("orderList",orderList);

        return "center-order-pay";
    }

    /**
     * 待发货订单
     */
    @RequestMapping("/orderSend")
    public String orderSend(Model model){

        //远程调用订单微服务,获取所有代发货订单
        List<Order> orderList = orderFeign.orderSend();
        model.addAttribute("orderList",orderList);

        return "center-order-send";
    }

    /**
     * 获取所有待收货订单
     * @param model
     * @return
     */
    @RequestMapping("/orderReceive")
    public String orderReceive(Model model){

        //远程调用订单微服务,获取所有代收货订单
        List<Order> orderList = orderFeign.orderReceive();
        model.addAttribute("orderList",orderList);

        return "center-order-receive";
    }

    /**
     * 确认收货
     * @param orderId
     * @return
     */
    @RequestMapping("/orderConfirm")
    public String orderConfirm(@RequestParam("orderId")String orderId){

        //远程调用订单微服务,根据订单id修改订单信息,确认收货
        orderFeign.orderConfirm(orderId);

        //重定向到待收货订单列表页面
        return "redirect:http://web.changgou.com:8001/api/wcenterorder/orderReceive";
    }

    /**
     * 提醒发货
     * @param orderId
     * @return
     */
    @RequestMapping("/remindSend")
    public String remindSend(@RequestParam("orderId")String orderId){

        //远程调用订单微服务实现提醒发货
        Result result = orderFeign.remindSend(orderId);
        boolean flag = result.isFlag();

        return "redirect:http://web.changgou.com:8001/api/wcenterorder/orderSend";
    }

}
